--但为求索
local danweiqiusuo = fk.CreateSkill{
    name = "mcdh_danweiqiusuo",
    tags = {Skill.Quest}
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

danweiqiusuo:addEffect('active',{
    target_num = 0,
    min_card_num = 1,
    can_use = function(self, player)
        return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:getQuestSkillState(self.name)
    end,
    card_filter = function(self,player, to_select, selected)
        if player:prohibitDiscard(Fk:getCardById(to_select)) then return end
        if #selected == 0 then
            return not table.contains(player:getTableMark("#danweiqiusuo-discard"), Fk:getCardById(to_select):getSuitString())
        else
            return table.every(selected,
                    function(id) return Fk:getCardById(to_select).suit ~= Fk:getCardById(id).suit end) and
                    not table.contains(player:getTableMark("#danweiqiusuo-discard"), Fk:getCardById(to_select):getSuitString())
        end
    end,
    on_use = function(self, room, effect)
        local player = effect.from
        local mark = player:getTableMark("#danweiqiusuo-discard")
        local suit = {}
        room:throwCard(effect.cards, self.name, player, player)
        for _, id in ipairs(effect.cards) do
            table.insertIfNeed(suit, Fk:getCardById(id):getSuitString())
        end
        table.insertTable(mark, suit)
        local cids = room:getNCards(#effect.cards * 2, "top")
        room:moveCardTo(cids, Card.Processing, nil, fk.ReasonJustMove, self.name)
        local cards = {}
        for _, id in ipairs(cids) do
            local card = Fk:getCardById(id)
            if table.contains(suit, card:getSuitString()) then
                table.insert(cards, id)
            end
        end
        if #cards > 0 then
            local dummy = Fk:cloneCard("slash")
            dummy:addSubcards(cards)
            room:obtainCard(player, dummy, true, fk.ReasonJustMove, player.id)
        end
        cids = table.filter(cids, function(id) return room:getCardArea(id) == Card.Processing end)
        room:moveCardTo(cids, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, self.name, nil, true, player.id)
        room:setPlayerMark(player, "#danweiqiusuo-discard", mark)
    end
})

danweiqiusuo:addEffect(fk.EventPhaseStart,{
    can_trigger = function(self, event, target, player, data)
        if target == player and player:hasSkill(self) and not player:getQuestSkillState("mcdh_danweiqiusuo") then
                if player.phase == Player.Finish then
                    return #player:getTableMark("#danweiqiusuo-discard") == 4
                end
        end
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
            room:updateQuestSkillState(player, "mcdh_danweiqiusuo", false)
            room:handleAddLoseSkills(player, "mcdh_bolankuangyu", nil, true, false)
    end,
})

Fk:loadTranslationTable {
    ["mcdh_danweiqiusuo"] = "但为求索",
    ["#mcdh_danweiqiusuo_trigger"] = "求知",
    [":mcdh_danweiqiusuo"] = "『但为求索』<br>使命技，出牌阶段限一次，你可以弃置任意张未以此法弃置的花色牌，然后从牌堆顶亮出双倍于弃置牌数的牌，获得其中与弃置牌花色相同的牌。" ..
            "成功：结束阶段，若你因此法弃置过的牌包含四种花色，你获得技能〖博览狂语〗。",
}

return danweiqiusuo